// pages/visitor/msg/msg.js
var api = require('../../../utils/httpUtil.js');
const app = getApp();
Component({
  options: {
    addGlobalClass: true,
  },
  data: {
    tabCur: 0,
    scrollLeft: 0,
    tabList: ['未读', '已读'],
    openId: null,
    imgUrl: api.imgUrl,
    unReadMsgList: [],
    readedMsgList: []
  },
  lifetimes: {
    // 组件生命周期函数，在组件布局完成后执行，此时可以获取节点信息   
    ready: function () {
      // 在组件布局完成后执行的操作
      this.setData({
        openId: app.globalData.openId
      })
      this.loadData();
    }
  },
  methods: {
    tabSelect(e) {
      this.setData({
        tabCur: e.currentTarget.dataset.id,
      })
    },
    msgClick(e) {
      //设置消息已读
      var id = e.currentTarget.dataset.id;
      this.readMsg(id);
      this.switchList(id);
      var msgList = this.data.readedMsgList.filter(item => item.id === id);
      var message = '';
      var date = '';
      var time = '';
      if (msgList.length > 0) {
        var msg = msgList[0];
        message = msg.message;
        date = msg.date;
        time = msg.time;
      }
      if (e.currentTarget.dataset.url) {
        wx.navigateTo({
          url: e.currentTarget.dataset.url + "?message=" + message + "&date=" + date + "&time=" + time,
        })
      }
    },
    loadData() {
      api.httpRequest(api.msgAPI + "/" + this.data.openId, {})
        .then(res => {
          if (res.code == 200) {
            var unReadList = res.data.filter(item => item.isRead === 0);
            unReadList.forEach(item => {
              item.date = item.createTime.substring(5, 10);
              item.time = item.createTime.substring(11, 16);
            });

            var readList = res.data.filter(item => item.isRead === 1);
            readList.forEach(item => {
              item.date = item.createTime.substring(5, 10);
              item.time = item.createTime.substring(11, 16);
            });

            this.setData({
              unReadMsgList: unReadList,
              readedMsgList: readList
            })
          }
        })
        .catch(error => {
          wx.showToast({
            title: error.message,
            duration: 1000,
            mask: true,
            icon: 'none'
          })
        });
    },
    //设置消息已读
    readMsg(id) {
      api.httpRequest(api.msgReadAPI + "/" + id, {})
        .then(res => {
          if (res.code != 200) {
            wx.showToast({
              title: res.msg,
              duration: 1000,
              mask: true,
              icon: 'none'
            })
          }
        })
        .catch(error => {
          wx.showToast({
            title: error.message,
            duration: 1000,
            mask: true,
            icon: 'none'
          })
        });
    },
    switchList(id) {
      var items_to_move = this.data.unReadMsgList.filter(item => item.id === id)
      var unReadList = this.data.unReadMsgList.filter(item => item.id != id)
      var readList = this.data.readedMsgList.concat(items_to_move)
      this.setData({
        unReadMsgList: unReadList,
        readedMsgList: readList
      })
    }
  }
})